//
//  BN_Mine_SelectPersonViewController.m
//  BabyNow
//
//  Created by 刘泽琛 on 14-5-23.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "BN_Mine_SelectPersonViewController.h"

@interface BN_Mine_SelectPersonViewController () {
    BN_MyInfoHelper *myInfoHelper_;
}

@end

@implementation BN_Mine_SelectPersonViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (IOS7) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"选择颁发";
    self.selectPersonTableView.backgroundColor = APP_OBJECT_GRAY_COLOR;
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"houtui"] style:UIBarButtonItemStylePlain target:self action:@selector(backRootVC)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    
    if ([self.selectPersonTableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.selectPersonTableView setSeparatorInset:UIEdgeInsetsZero];
    }
    self.selectPersonTableView.delegate = self;
    self.selectPersonTableView.dataSource = self;
    // 清楚多余边框
    [self setExtraCellLineHidden:self.selectPersonTableView];
    switch (MAIN_VERSION) {
        case 6:
            _selectPersonTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
            _selectPersonTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-44);
            break;
        default:
            _selectPersonTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, FULLSCREEN_HEIGHT);
            _selectPersonTableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
            _selectPersonTableView.scrollIndicatorInsets = UIEdgeInsetsMake(64, 0, 0, 0);
            break;
    }
    
    // 网络类
    myInfoHelper_ = [[BN_MyInfoHelper alloc]init];
    [self performSelectorInBackground:@selector(requestNetData) withObject:nil];
    
    [self viewLoadHintView];
}

#pragma mark 网络请求

- (void)requestNetData {
    __weak typeof(self) weakSelf = self;
    [myInfoHelper_ queryChildList:^(id result, NSString *errMsg) {
        if (errMsg) {
            [LZCHINT title:errMsg type:LZCError duration:2 places:HINT_PLACES callBack:NULL];
            [weakSelf cancelHintView];
        }else{
            weakSelf.babyListArr = (NSArray *)result;
            [weakSelf.selectPersonTableView reloadData];
            [weakSelf cancelHintView];
        }
    }];
}

#pragma mark
#pragma mark UITableView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.babyListArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"BN_Mine_SelectPersonViewController";
    BN_IssueBabyCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"BN_IssueBabyCell" owner:self options:nil]objectAtIndex:0];
    }
    cell.tag = [self.babyListArr[indexPath.row][@"chilId"] integerValue];
    cell.textLabel.font = [UIFont systemFontOfSize:14.0f];
    cell.nameLabel.text = self.babyListArr[indexPath.row][@"chilName"];

    // 提示信息
    NSString *medalStr = [NSString stringWithFormat:@"%@",self.babyListArr[indexPath.row][@"medaDate"]];
    NSString *commentStr = [NSString stringWithFormat:@"%@",self.babyListArr[indexPath.row][@"evalDate"]];

    if (![medalStr isEqual:[NSNull null]] && ![medalStr isEqualToString:@"<null>"]) {
        if (![commentStr isEqual:[NSNull null]]) {
            cell.oneImgView.image = [UIImage imageNamed:@"ismedal"];
            cell.twoImgView.image = [UIImage imageNamed:@"iscomment"];
        }else {
            cell.twoImgView.image = [UIImage imageNamed:@"ismedal"];
        }
    }else {
        if (![commentStr isEqual:[NSNull null]]) {
            cell.twoImgView.image = [UIImage imageNamed:@"iscomment"];
        }else {
            cell.twoImgView.image = [[UIImage alloc]init];
        }
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *chilId = [NSString stringWithFormat:@"%ld",(long)cell.tag];
    self.chilId = chilId;
    [self openMineMedalAndCommentVc];
}

- (void)openMineMedalAndCommentVc {
    BN_MINE_MedalAndCommentViewController *medalAndCommentVC = [[BN_MINE_MedalAndCommentViewController alloc]init];
    medalAndCommentVC.chilIdStr = self.chilId;
    [self.navigationController pushViewController:medalAndCommentVC animated:YES];
}

#pragma mark Action
- (void)backRootVC {
    [self releaseResource];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark
#pragma mark Block
- (void)transmitBabyInfo:(TransmitBabyInfo)info {
    self.babyInfo = info;
}

#pragma mark
#pragma mark 资源

- (void)releaseResource {
    self.selectPersonTableView.delegate = nil;
    self.selectPersonTableView.dataSource = nil;
    self.selectPersonTableView = nil;
}

- (void)dealloc {
    NSLog(@"我的-选择颁发宝宝 释放...");
}
@end
